<%--
  Created by IntelliJ IDEA.
  User: teacher
  Date: 2023/11/9
  Time: 9:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>

<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <title>backend</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/bootstarp/css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/index.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/nmms.css"/>
    <script src="${pageContext.request.contextPath}/js/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/bootstarp/js/bootstrap.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/userSetting.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap-paginator.js" type="text/javascript"
            charset="utf-8"></script>
    <script>
        $(function () {
            // 使用Bootstrap分页插件对后台传递过来的分页数据实现分页功能
            // 共分为三步
            // 1.引入分页插件的js:bootstrap-paginator.js
            // 2.在页面中需要显示页码的位置定义一个ul标签,用于展示页码的相关信息
            // 3.在对于元素中调用分页方法,方法名就是分页插件js的名字,驼峰命名法
            // 方法的参数是一个option选项,类似于ajax方法参数
            $("#productTypePage").bootstrapPaginator({
                // 指定bootstrap版本号,在3这个版本之前与3开始的版本用法不一致
                // 早期使用的div接收的,3开始使用的ul
                // 默认是3之前的版本
                bootstrapMajorVersion: 3,
                // 指定当前页
                // 当没有数据的时候,由于当前页与总页数变量类型是int类型
                // 其默认值为0,而对于页码而言,不存在第0页的概念
                // 如果出现第0页,会报错:Uncaught Page out of range
                // 因此,需要手动将默认值该为1
                currentPage: "${typePage.pageNum == 0 ? 1 : typePage.pageNum}",
                // 总页数
                totalPages: "${typePage.pages == 0 ? 1 : typePage.pages}",
                // 样式
                size: "normal",
                alignment: "center",
                // 分页事件
                // 当用户点击页码后触发的事件
                // 分为两种事件
                // 1.无参分页事件
                pageUrl: function (type, page, current) {
                    // 返回值为当点击页码之后跳转的地址
                    return "${pageContext.request.contextPath}/type/findAll.do?pageNo=" + page;
                }
                // 2.有参分页事件
            })

            // 添加商品类型
            $(".addProductType").click(function () {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/type/addType.do",
                    data: {"name": $("#productTypeName").val()},
                    dataType: "json",
                    success: function (result) {
                        // 接收到响应信息之后,根据业务逻辑状态码的不同进行不同的处理
                        // 一般分为成功与失败的两种处理
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/type/findAll.do?pageNo=${typePage.pageNum == 0 ? 1 : typePage.pageNum}";
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                })
            });

            // 修改设置默认值
            $(".doProTypeModify").click(function(){
                $.ajax({
                    type:"post",
                    url:"${pageContext.request.contextPath}/type/findById.do",
                    data:{"id":$(this).attr("data-id")},
                    dataType:"json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            $("#proTypeNum").val(result.returnObject.id);
                            $("#proTypeName").val(result.returnObject.name);
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                })
            });

            // 修改类型名称
            $(".updateProType").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/type/modifyName.do",
                    data: {
                        "id":$("#proTypeNum").val(),
                        "name":$("#proTypeName").val()
                    },
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/type/findAll.do?pageNo=${typePage.pageNum == 0 ? 1 : typePage.pageNum}";
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                })
            });

            // 启用与禁用
            $("input[name='modifyStatus']").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/type/modifyStatus.do",
                    data: {"id":$(this).attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/type/findAll.do?pageNo=${typePage.pageNum == 0 ? 1 : typePage.pageNum}";
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                })
            });
        });
    </script>
</head>

<body>
<div class="panel panel-default" id="userSet">
    <div class="panel-heading">
        <h3 class="panel-title">商品类型管理&nbsp;&nbsp;&nbsp;&nbsp;<span id="errorMsg"></span></h3>
    </div>
    <div class="panel-body">
        <input type="button" value="添加商品类型" class="btn btn-primary" id="doAddProTpye">
        <div class="modal fade" tabindex="-1" id="ProductType">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">添加商品类型信息</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="productTypeName" class="col-sm-4 control-label">类型名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="productTypeName">
                            </div>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary addProductType">添加</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>
        <br>
        <br>
        <div class="show-list">
            <table class="table table-bordered table-hover" style='text-align: center;'>
                <thead>
                <tr class="text-danger">
                    <th class="text-center">编号</th>
                    <th class="text-center">类型名称</th>
                    <th class="text-center">状态</th>
                    <th class="text-center">操作</th>
                </tr>
                </thead>
                <tbody id="tb">
                <c:forEach items="${typePage.list}" var="type">
                    <tr>
                        <td>${type.id}</td>
                        <td>${type.name}</td>
                        <td>
                            <c:if test="${type.status == 1}">启用</c:if>
                            <c:if test="${type.status == 0}">禁用</c:if>
                        </td>
                        <td class="text-center">
                            <input type="button" class="btn btn-warning btn-sm doProTypeModify" data-id="${type.id}" value="修改">
                            <c:if test="${type.status == 1}">
                                <input type="button" class="btn btn-danger btn-sm doProTypeDisable" name="modifyStatus" data-id="${type.id}" value="禁用">
                            </c:if>
                            <c:if test="${type.status == 0}">
                                <input type="button" class="btn btn-success btn-sm doProDisable" name="modifyStatus" data-id="${type.id}" value="启用">
                            </c:if>

                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            <ul id="productTypePage"></ul>
        </div>
        <div class="modal fade" tabindex="-1" id="myProductType">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">商品修改</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="proTypeNum" class="col-sm-4 control-label">编号：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="proTypeNum" readonly>
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="proTypeName" class="col-sm-4 control-label">类型名称</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="proTypeName">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-warning updateProType">修改</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

</html>